package cn.itsource.org.service.impl;

import cn.itsource.basic.base.domain.RegisterDto;
import cn.itsource.basic.exception.CustomException;
import cn.itsource.basic.exception.ErrorCode;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.basic.util.MD5Utils;
import cn.itsource.basic.util.StrUtils;
import cn.itsource.org.domain.Logininfo;
import cn.itsource.org.domain.User;
import cn.itsource.org.mapper.LogininfoMapper;
import cn.itsource.org.mapper.UserMapper;
import cn.itsource.org.service.IUserService;
import cn.itsource.basic.base.service.impl.BaseServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author h
 * @since 2023-07-09
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {

    @Autowired
    private RedisTemplate<Object,Object> redisTemplate;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;

    @Override
    @Transactional
    public AjaxResult register(RegisterDto dto) {
        //取数据
        String phone = dto.getPhone();
        String smsCode = dto.getSmsCode();
        String password = dto.getPassword();
        String confirmPassword = dto.getConfirmPassword();
        //判断两次密码是否一致
        if(!password.equals(confirmPassword)){
            throw new CustomException(ErrorCode.PWD_INCONSISTENT.getCode());
        }
        //查询redis中得短信验证码
        String smsCodeKey = "smsCode:"+phone;
        Object value = redisTemplate.opsForValue().get(smsCodeKey);
        //验证码过期判断
        if(value == null){
            throw new CustomException(ErrorCode.SMS_CODE_EXPIRE.getCode());
        }
        //redis中得短信验证码
        String redisSmsCode = value.toString().split(":")[0];
        if (!smsCode.equals(redisSmsCode)){
            throw new CustomException(ErrorCode.SMS_CODE_ERROR.getCode());
        }
        //判断用户是否已经存在
        User user = userMapper.loadByPhone(phone);
        //用户已存在判断
        if(user != null){
            throw new CustomException(ErrorCode.USER_ALREADY_EXIST.getCode());
        }
        //密码加密
        String salt = StrUtils.getComplexRandomString(64);
        String resultPwd = MD5Utils.encrypByMd5(password+salt);
        //保存用户注册数据 构建user对象，构建loginInfo对象 先保存loginINfo 再保存 User
        //初始化user对象
        User resultUser = innitUser(phone, salt, resultPwd);
        //初始化logininfo对象
        Logininfo logininfo = user2Logininfo(resultUser);
        //保存lf
        logininfoMapper.insert(logininfo);
        //保存user
        resultUser.setLogininfoId(logininfo.getId());
        userMapper.insert(resultUser);
        return AjaxResult.me();
    }

    public static Logininfo user2Logininfo(User resultUser) {
        Logininfo lf = new Logininfo();
        BeanUtils.copyProperties(resultUser,lf);
        lf.setType(1);
        lf.setDisable(1);
        return lf;
    }

    public static User innitUser(String phone, String salt, String resultPwd) {
        User newUser = new User();
        newUser.setPhone(phone);
        newUser.setUsername(phone);
        newUser.setCreatetime(new Date());
        newUser.setState(0);
        newUser.setPassword(resultPwd);
        newUser.setSalt(salt);
        return newUser;
    }
}
